﻿#include <iostream>
#include <string>
#include <unordered_map>

bool check(int n) {
    for (int i = 2; i < n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

int main() {
    try {
        std::string a, b, c;
        std::cin >> a >> b >> c;

        std::unordered_map<std::string, int> L = { {"zero", 0}, {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4}, {"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9} };
        int n = L[a] * 100 + L[b] * 10 + L[c];

        if (std::to_string(n).length() != 3) {
            throw 1;
        }

        int countup[2] = { 0, 0 };
        int countdown[2] = { 0, 0 };

        for (int i = n; i <= n * 2; i++) {
            if (check(i)) {
                countup[1] = i;
                break;
            }
            else {
                countup[0]++;
            }
        }

        for (int i = n; i >= 100; i--) {
            if (check(i)) {
                countdown[1] = i;
                break;
            }
            else {
                countdown[0]++;
            }
        }

        if (countdown[0] <= countup[0]) {
            std::cout << countdown[1] << std::endl;
        }
        else {
            std::cout << countup[1] << std::endl;
        }
    }
    catch (...) {
        std::cout << "wrong input" << std::endl;
    }

    return 0;
}
